import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js';
import axios from "../../http/axios.js";
import {
  apiAddAddingEmergencyContacts,
  apiEditAddingEmergencyContacts
} from "../../http/server.js";
import {
  regExpName,
  regExpPhone
} from "../../utils/regular.js";
Page({
  data: {
    contactId: "1", // 编辑联系人的id
    type: "add", // 添加 | 编辑联系人
    van_pickerShow: false, // 选择器显示隐藏
    columns: ["男", "女"], // 选择器组件
    // columns: ["本人", "父母", "子女", "亲戚", "朋友"], // 选择器组件
    contact: {
      titleOne: {
        id: 1,
        value: "贾伟",
        placeholder: "请输入紧急联系人姓名"
      },
      titleTwo: {
        id: 2,
        value: "1393531102",
        placeholder: "请输入紧急联系人手机号码"
      },
      titleThree: {
        id: 1,
        key: "请选择紧急联系人性别",
        sex: "男"
      },
      titleFour: {
        id: 2,
        key: "请选择与紧急联系人的关系",
        relationship: "父母"
      }
    },
    idIndex: 4, // 初始弹出的索引值
    id: "1" // 当前选择的是哪个栏目的选择
  },
  onLoad: function(options) {
    let type = options.type;
    if (type == "add") { // 从联系人到添加联系人界面
      let contact = this.data.contact;
      contact = {
        ...contact,
        titleOne: {
          id: 1,
          value: "",
          placeholder: "请输入紧急联系人姓名"
        },
        titleTwo: {
          id: 2,
          value: "",
          placeholder: "请输入紧急联系人手机号码"
        },
      }
      this.setData({
        contact: contact,
        type: type
      })
    } else { // type == "edit" 编辑
      let item = JSON.parse(options.item);
      let contact = {
        titleOne: {
          id: 1,
          value: item.name,
          placeholder: "请输入紧急联系人姓名"
        },
        titleTwo: {
          id: 2,
          value: item.phone,
          placeholder: "请输入紧急联系人手机号码"
        },
        titleThree: {
          id: 1,
          key: item.sex,
          sex: "男"
        },
        titleFour: {
          id: 2,
          key: item.relation,
          relationship: "父母"
        }
      }
      this.setData({
        contact: contact,
        type: type,
        contactId: item.contactId
      })
    }
  },
  onReady: function() {},
  onShow: function() {},
  // 校验
  check: function() {
    let contact = this.data.contact;
    let name = contact.titleOne.value;
    let phone = contact.titleTwo.value;
    let sex = contact.titleThree.key;
    let relationship = contact.titleFour.key;
    if (!name.length) {
      wx.showToast({
        title: "请输入紧急联系人姓名",
        icon: "none"
      })
      return false;
    }
    if (!regExpName.test(name)) {
      wx.showToast({
        title: "请您输入正确的姓名",
        icon: "none"
      })
      return false;
    }
    if (!phone.length) {
      wx.showToast({
        title: "请输入紧急联系人手机号码",
        icon: "none"
      })
      return false;
    }
    if (!regExpPhone.test(phone)) {
      wx.showToast({
        title: "请您输入正确的手机号",
        icon: "none"
      })
      return false;
    }
    if (sex == "请选择紧急联系人性别") {
      wx.showToast({
        title: "请选择紧急联系人性别",
        icon: "none"
      })
      return false;
    }
    if (relationship == "请选择与紧急联系人的关系") {
      wx.showToast({
        title: "请选择与紧急联系人的关系",
        icon: "none"
      })
      return false;
    }
    return {
      name: name,
      phone: phone,
      sex: sex,
      relationship: relationship
    };
  },
  // 点击保存
  handlClickSuccess: function() {
    if (!this.check()) {
      return;
    }
    let item = this.check();
    let type = this.data.type;
    let userInfo = wx.getStorageSync("userInfo");
    if (type == "add") { // 新增
      let data = {
        "workersId": userInfo.workersId, // 职工id
        "name": item.name, // 紧急联系人姓名
        "phone": item.phone, // 紧急联系人手机号
        "sex": item.sex == "男" ? 1 : 2, // 紧急联系人性别 1 男 2 女
        "relation": item.relationship // 与职工的关系 本人、父母、子女、亲戚、朋友
      }
      axios({
        url: apiAddAddingEmergencyContacts,
        method: "POST",
        data: data
      }).then(res => {
        wx.showToast({
          title: "添加成功",
          icon: "none"
        })
        setTimeout(() => {
          wx.navigateBack({
            url: "/pages/n_urgentContacts/n_urgentContacts"
          })
        }, 1500)
      })
    } else { //编辑
      let data = {
        "contactId": this.data.contactId,
        "workersId": userInfo.workersId, //职工id
        "name": item.name, //紧急联系人姓名
        "phone": item.phone, //紧急联系人手机号
        "sex": item.sex == "男" ? 1 : 2, //紧急联系人性别 1 男 2 女
        "relation": item.relationship //与职工的关系 本人、父母、子女、亲戚、朋友
      }
      axios({
        url: apiEditAddingEmergencyContacts + `/${this.data.contactId}`,
        method: "PUT",
        data: data
      }).then(res => {
        wx.showToast({
          title: "编辑成功",
          icon: "none"
        })
        setTimeout(() => {
          wx.navigateBack({
            url: "/pages/n_urgentContacts/n_urgentContacts"
          })
        }, 1000)
      })
    }
  },
  // 点击确定时候的处理
  onConfirm: function(e) {
    let id = this.data.id;
    let value = e.detail.value;
    let contact = this.data.contact;
    if (id == 1) { //选择性别
      contact = {
        ...contact,
        titleThree: {
          id: 1,
          key: value,
          // sex: "男"
        }
      }
      this.setData({
        contact: contact,
        van_pickerShow: false
      })
    } else { // 选择关系
      contact = {
        ...contact,
        titleFour: {
          id: 2,
          key: value,
          // relationship: "父母"
        }
      }
      this.setData({
        contact: contact,
        van_pickerShow: false
      })
    }
  },
  // 点击蒙版关闭弹出框
  onClose: function() {
    this.setData({
      van_pickerShow: false
    })
  },
  // 点击取消隐藏弹出框
  onCancel: function() {
    this.setData({
      van_pickerShow: false
    })
  },
  // 选择信息
  handleClickSelect: function(e) {
    // 可能还会存在bug 等待测试
    // this.setData({
    //   columns: ["本人", "父母", "子女", "亲戚", "朋友"],
    //   idIndex: 4,
    //   van_pickerShow: true
    // })
    // return;
    let id = e.detail;
    // console.log(id);
    // id是1代表选择紧急联系人性别
    if (id == 1) {
      let keyValue = this.data.contact.titleThree.key;
      let columns = ["男", "女"];
      columns.forEach((item, index) => {
        if (item == keyValue) {
          this.setData({
            idIndex: Number(index)
          })
        }
      })
      this.setData({
        id: id,
        columns: columns,
        van_pickerShow: true
      });
    } else { // id是2代表选择与紧急联系人关系
      let keyValue = this.data.contact.titleFour.key;
      let columns = ["本人", "父母", "子女", "亲戚", "朋友"];
      columns.forEach((item, index) => {
        if (item == keyValue) {
          this.setData({
            idIndex: 3,
            van_pickerShow: true
          })
          return;
          this.setData({
            idIndex: Number(index)
          })
        }
      })
      this.setData({
        id: id,
        columns: columns,
        van_pickerShow: true
      });
    }
  },
  // 输入框输入改变
  bindInput: function(e) {
    let contact = this.data.contact;
    let item = e.detail;
    if (item.id == 1) { //紧急联系人姓名
      contact = {
        ...contact,
        titleOne: item
      }
    } else { //紧急联系人手机号码
      contact = {
        ...contact,
        titleTwo: item
      }
    }
    this.setData({
      contact: contact
    })
  }
})